package com.salton123.imagepicker.ui

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.salton123.imagepicker.ImagePicker
import com.salton123.imagepicker.bean.ImageItem
import com.salton123.imagepicker.custom.CallBackRepository
import com.salton123.imagepicker.custom.ImagePickerUtil
import com.salton123.imagepicker.custom.ImagePickerUtil.REQUEST_CODE_PICK
import com.salton123.imagepicker.custom.ImagePickerUtil.REQUEST_CODE_PREVIEW
import com.salton123.imagepicker.custom.ImagePickerUtil.REQUEST_CODE_TAKE
import com.salton123.imagepicker.util.BitmapUtil
import java.util.ArrayList

/**
 *Created by panzhiwei on 2020-03-05
 */
class ImagePickerFragment : Fragment() {
    companion object {
        const val TAG = "ImagePickerFragment"
    }

    var selectList: ArrayList<ImageItem>? = null ////用来保存已添加的图片集合,接收图册返回数据。
    var imagePickerCallback: CallBackRepository.IImagePickerCallback? = null
    var requestCode: Int? = null
    var previewPosition: Int? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        handleIntent()
    }


    private fun handleIntent() {
        try {
            when (requestCode) {
                REQUEST_CODE_PICK -> {
                    val intent = Intent(context, ImageGridActivity::class.java)
                    intent.putExtra(ImageGridActivity.EXTRAS_IMAGES, selectList)
                    startActivityForResult(intent, REQUEST_CODE_PICK)
                }
                REQUEST_CODE_TAKE -> {
                    val intent = Intent(context, ImageGridActivity::class.java)
                    intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true) // 是否是直接打开相机
                    intent.putExtra(ImageGridActivity.EXTRAS_IMAGES, selectList)
                    startActivityForResult(intent, REQUEST_CODE_TAKE)
                }
                REQUEST_CODE_PREVIEW -> {
                    val intent = Intent(context, ImagePreviewDelActivity::class.java)
                    intent.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, selectList)
                    intent.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, previewPosition)
                    intent.putExtra(ImagePicker.EXTRA_FROM_ITEMS, true)
                    startActivityForResult(intent, REQUEST_CODE_PREVIEW)
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val list = ImagePickerUtil.onActivityResult(requestCode, resultCode, data).apply {
            this?.forEach {
                if ((BitmapUtil.getBitmapDegree(it.path) == 90 || BitmapUtil.getBitmapDegree(it.path) == 180)
                    && it.width > it.height
                ) {
                    val temp = it.width
                    it.width = it.height
                    it.height = temp
                }
            }
        }
        imagePickerCallback?.callback(list)
        reset()
    }

    private fun reset() {
        selectList = null
        imagePickerCallback = null
        requestCode = null
        previewPosition = null
    }
}